package com.itheima.reggie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domian.Dish;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.utils.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 何须浅碧深红色
 * @date 2022/9/13
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CacheService cacheService;
//    @Autowired
//    private DishMapper dishMapper;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.dishWithFlavor(dishDto);
        Set<String> keys = cacheService.keys("dish_:*");
        cacheService.delete(keys);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
//        Page pageInfo = new Page(page,pageSize);
////        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
////        wrapper.like(name!=null,Dish::getName,name);
////        wrapper.orderByDesc(Dish::getCreateTime);
////        dishService.page(pageInfo,wrapper);
//        List<DishDto> byPage = dishMapper.findByPage(pageInfo, name);
//        Page records = pageInfo.setRecords(byPage);
//        return R.success(records);
       return dishService.selectAll(page,pageSize,name);
    }
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getDishByIdWithFlavor(id);
        return R.success(dishDto);
    }

//    @GetMapping("/{id}")
//    public R<DishDto> get(@PathVariable Long id){
//        DishDto dishDto = dishService.getByIdWithFlavor(id);
//        return R.success(dishDto);
//    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        Set<String> keys = cacheService.keys("dish_:*");
        cacheService.delete(keys);
        return R.success("修改菜品成功");
    }
    //套餐管理里面的根据getCategoryId查询
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
//        List<DishDto> dtos = null;
        String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
        String result = cacheService.get(key);
        if (result != null){
            List<DishDto> dishDtos = JSON.parseArray(result, DishDto.class);
            return R.success(dishDtos);
        }
        R<List<DishDto>> findwithFlavor = dishService.findwithFlavor(dish);
        cacheService.setEx(key,JSON.toJSONString(findwithFlavor.getData()),60, TimeUnit.MINUTES);

//        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getCreateTime);
//        List<Dish> list = dishService.list(wrapper);
//        List<DishDto> dtos = new ArrayList<>();
//        for (Dish dishlist : list) {
//            //创建一个DishDto对象
//            DishDto dto = new DishDto();
//            //将dto里面的数据拷贝到DishDto上面 这时候就dto里面就只差一个口味去了
//            BeanUtils.copyProperties(dishlist,dto);
//            //根据每个商品拿到他的id值
//            Long dishId = dishlist.getId();
//            //然后根据菜品id拿到他的口味信息的集合
//            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
//            //条件控制器
//            queryWrapper.eq(DishFlavor::getDishId,dishId);
//            List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);
//            dto.setFlavors(dishFlavors);
//            dtos.add(dto);
//        }
//        return R.success(dtos);
     return findwithFlavor;
    }
//    @PostMapping("/status/{status}")
//    public R<Integer> status(Dish dish){
////        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
////        wrapper.eq(dish.getId()!=null,Dish::getId,dish.getId());
//        dishService.updateById(dish);
//        return R.success(dish.getSort());
//    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        //1,根据参数拿到一个ids集合 然后根据id去删除对象，但是需要判断是不是在起售状态
        dishService.delete(ids);
        Set<String> keys = cacheService.keys("dish_:*");
        cacheService.delete(keys);
        return R.success("删除成功");
    }

}
